#!/usr/bin/env python
# $Id: getgen.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

# GET Command Generator
from pysnmp.entity import engine, config
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity.rfc3413 import cmdgen

snmpEngine = engine.SnmpEngine()

# v1/2 setup
# config.addV1System(snmpEngine, 'test-agent', 'public')
#
"""
      config.usm3DESEDEPrivProtocol
      config.addTargetParams(               config.usmAesCfb128Protocol
      config.addTrapUser(                   config.usmAesCfb192Protocol
      config.addV1System(                   config.usmAesCfb256Protocol
      config.addV3User(                     config.usmDESPrivProtocol
      config.addVacmAccess(                 config.usmHMACMD5AuthProtocol
      config.addVacmGroup(                  config.usmHMACSHAAuthProtocol
      config.addVacmUser(                   config.usmNoAuthProtocol
      config.addVacmView(                   config.usmNoPrivProtocol

"""

# v3 setup
config.addV3User(
    snmpEngine, 'ciscosa',
    config.usmHMACSHAAuthProtocol, 'z04gRRzf',
    # config.usmDESPrivProtocol, 'yrqehp4z'
    config.usmAesCfb128Protocol, 'yrqehp4z'
    )

# Transport params
config.addTargetParams(snmpEngine, 'myParams', 'ciscosa', 'authPriv')
#config.addTargetParams(snmpEngine, 'myParams', 'test-agent', 'noAuthNoPriv', 1)

# Transport addresses
config.addTargetAddr(
    snmpEngine, 'myRouter', config.snmpUDPDomain,
    ('172.29.17.1', 161), 'myParams'
    )

# Setup transport endpoint
config.addSocketTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpSocketTransport().openClientMode()
    )

def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex,
          varBinds, cbCtx):
    cbCtx['errorIndication'] = errorIndication
    cbCtx['errorStatus'] = errorStatus
    cbCtx['errorIndex'] = errorIndex
    cbCtx['varBinds'] = varBinds

# Used to pass data from callback function
cbCtx = {}
    
cmdgen.GetCommandGenerator().sendReq(
    snmpEngine, 'myRouter', (((1,3,6,1,2,1,1,1,0), None),), cbFun, cbCtx
    )

snmpEngine.transportDispatcher.runDispatcher()
if cbCtx['errorIndication']:
    print cbCtx['errorIndication']
elif cbCtx['errorStatus']:
    print cbCtx['errorStatus'].prettyPrint()
else:
    for oid, val in cbCtx['varBinds']:
        print '%s = %s' % (oid.prettyPrint(), val.prettyPrint())
